#import "OAmapPlugin.h"
/// 地图基础
#import <AMapFoundationKit/AMapFoundationKit.h>
/// 基础模块
#import "FlutterIosMapViewFactory.h"
#import "HQMapManager.h"
#import "HQLocationManager.h"
#import "HQNaviManager.h"
#import "HQSearchManager.h"
#import "GdMapFlutterStreamManager.h"


@interface OAmapPlugin()

/// 方法数组<之后不同类型下添加方法，到相应的数组中添加方法名>
@property (nonatomic, strong) NSArray *mapMethods;
@property (nonatomic, strong) NSArray *locationMethods;
@property (nonatomic, strong) NSArray *searchMethods;
@property (nonatomic, strong) NSArray *naviMethods;

@end

@implementation OAmapPlugin

#pragma mark -- Plugin

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

    OAmapPlugin *instance = [[OAmapPlugin alloc] init];
    GdMapFlutterStreamHandler *handler = [[GdMapFlutterStreamManager shareInstance] steamHandler];
    
    /// 地图设置 apikey
    FlutterMethodChannel *searchChannel = [FlutterMethodChannel methodChannelWithName:@"com.p595263720.zit/appkey" binaryMessenger:[registrar messenger]];
    [registrar addMethodCallDelegate:instance channel:searchChannel];
    
    /// 注册地图 FlutterIosMapViewFactory
    /// com.p595263720.zit/GD 为 flutter 调用此 mapView 的标识
    FlutterIosMapViewFactory *mapViewFactory = [[FlutterIosMapViewFactory alloc] initWithRegistrar:registrar withMapPlugin:instance streamHandler:handler];
    [registrar registerViewFactory:mapViewFactory withId:@"com.p595263720.zit/GD"];
    /// 定位
    [[HQLocationManager shareInstance] locationWithRegistrar:registrar withMapPlugin:instance streamHandler:handler];
    /// 导航
    [[HQNaviManager shareInstance] naviWithRegistrar:registrar withMapPlugin:instance];
    /// 搜索
    [[HQSearchManager shareInstance] searchWithRegistrar:registrar withMapPlugin:instance];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    
    if([call.method isEqualToString:@"setMapAppKey"]) {
        NSLog(@"MapAppKey = %@", call.arguments);
        /// 设置appkey
        [[AMapServices sharedServices] setEnableHTTPS:YES];
        [AMapServices sharedServices].apiKey = call.arguments;
        
    } else if ([self.locationMethods containsObject:call.method]) {
        /// 定位相关
        [[HQLocationManager shareInstance] locationMethodCall:call result:result];
        
    } else if ([self.mapMethods containsObject:call.method]) {
        /// 地图
        [[HQMapManager shareInstance] mapViewMethodCall:call result:result];
        
    } else if ([self.naviMethods containsObject:call.method]) {
        /// 导航
        [[HQNaviManager shareInstance] naviMethodCall:call result:result];
        
    } else if ([self.searchMethods containsObject:call.method]) {
        /// 搜索
        [[HQSearchManager shareInstance] searchMethodCall:call result:result];
        
    } else {
        
        result(FlutterMethodNotImplemented);
    }
}

#pragma mark -- 懒加载

- (NSArray *)mapMethods {
    if (!_mapMethods) {
        _mapMethods = @[@"setMapType", @"setZoomLevel", @"setPosition", @"changeLatLng", @"getCenterPoint",
                        @"showIndoorMap", @"setUiSettings", @"setMyLocationStyle", @"addMarker", @"addMarkers",
                        @"clearMarkers", @"clearMap", @"addPolyline", @"setPointToCenter", @"setShowArea",
                        @"map#setCustomMapStylePath", @"map#setMapCustomEnable", @"map#setCustomMapStyleID", @"map#setLanguage",
                        @"map#zoomToSpan", @"map#setMapStatusLimits"
        ];
    }
    return _mapMethods;
}

- (NSArray *)locationMethods {
    if (!_locationMethods) {
        _locationMethods = @[@"startLocation", @"stopLocation", @"destroyLocation", @"coordinateTransformation", @"calculateLineDistance"];
    }
    return _locationMethods;
}

- (NSArray *)naviMethods {
    if (!_naviMethods) {
        _naviMethods = @[@"calculateDriveRoute", @"startNav"];
    }
    return _naviMethods;
}

- (NSArray *)searchMethods {
    if (!_searchMethods) {
        _searchMethods = @[@"searchPoi", @"searchPoiBound", @"searchReGeocode", @"inputTips", @"searchGeocode",
                           @"searchPoiPolygon", @"searchPoiId", @"searchRoutePoiLine", @"searchRoutePoiPolygon",
                           @"calculateDriveRoute", @"distanceSearch", @"searchBusStation", @"searchBusLine"
        ];
    }
    return _searchMethods;
}

@end
